package com.ohs.dal.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.ohs.dal.dao.VideoDAO;
import com.ohs.dal.model.Location;
import com.ohs.dal.model.Tag;
import com.ohs.dal.model.Video;

public class VideoDAOImpl extends AbstractDAOImpl<Video> implements VideoDAO {
	public List<Video> list() {
		return getHibernateTemplate().find("from " + Video.class.getName());
	}

	public Video get(Long videoId) {
		return (Video) getHibernateTemplate().get(Video.class, videoId);
	}

	public List<Tag> getTags() {
		return getHibernateTemplate().find("from " + Tag.class.getName());
	}

	public List<Video> getTopVideosByTag(Long tagId, int topSize) {
		String queryString = "select v from Video v join v.tags vt where vt.id=:tagId order by v.createdDate desc";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
		query.setFirstResult(0);
		query.setMaxResults(topSize);
		query.setParameter("tagId", tagId);
		return (List<Video>) query.list();
	}

	public Tag getTag(Long tagId) {
		return (Tag) getHibernateTemplate().get(Tag.class, tagId);
	}
	
	public List<Video> getVideosByTag(Long tagId) {
		return getHibernateTemplate().find("select v from Video v join v.tags vt where vt.id=" + tagId + " order by v.createdDate desc");
	}

	public List<Video> getLatestVideos(int topSize) {
		String queryString = "select v from Video v order by RAND()";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(queryString);
		query.setFirstResult(0);
		query.setMaxResults(topSize);
		return (List<Video>) query.list();
	}
}
